package com.hq.fxh.Vidoe;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.tencent.upload.Const;
import com.tencent.upload.UploadManager;
import com.tencent.upload.task.ITask;
import com.tencent.upload.task.IUploadTaskListener;
import com.tencent.upload.task.VideoAttr;
import com.tencent.upload.task.data.FileInfo;
import com.tencent.upload.task.impl.VideoUploadTask;

import java.util.Date;


/**
 * @author 小谢
 * @ClassName UpLoadVideoUtils
 * @Description :
 * @Created 2017/4/6 0006
 */
public class UpLoadVideoUtils {
    private static VideoUploadTask videoUploadTask = null;
     private static Handler mMainHandler;
    private static UploadManager mFileUploadManager = null;

    public static void upLoadCoverImg(final Context context, final String title, Bitmap bitmap) {
        String videoCoverImgurl ="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1028207068,3081587480&fm=111&gp=0.jpg";
        upLoadVideo(context, title, true, videoCoverImgurl);
    }

    private static void upLoadVideo(final Context context, final String title, boolean to_over_write, String videoCoverurl) {

        String appid = "10012792";
        String bucket = "video";
        String persistenceId = "videPersistenceId";
        mFileUploadManager = new UploadManager(context, appid, Const.FileType.Video, persistenceId);

        int uid = 24727;
        VideoAttr videoAttr = new VideoAttr();
        videoAttr.isCheck = false;
        String timestamp = new Date().getTime() + "" + (int) (Math.random() * 1000);
        videoAttr.title = title;
        videoAttr.timeLen = 213;
        videoAttr.desc = "";
      //  String mFilePath = "/storage/emulated/0/Tencent/QQfile_recv/2242031708171b9ea1a31091.mp4";
        String mFilePath =  "/storage/emulated/0/Tencent/QQfile_recv/2242031708171b9ea1a31091.mp4";

        String nameend = mFilePath.substring(mFilePath.lastIndexOf("."));//后缀
        UpLoadVideoRes upLoadVideoRes = new UpLoadVideoRes(uid, title, videoCoverurl, ""+ videoAttr.timeLen );
        final String videoUploadInfoJson = new Gson().toJson(upLoadVideoRes) ;

        Log.e("eeeeeeeeeeee", "bucket:" + bucket + "   mfilePath:" + mFilePath + "    path: 是 " + "/Android/" + timestamp + nameend + "    videoAttr: " + videoAttr + "    to_Over_write:" + to_over_write);

        videoUploadTask = new VideoUploadTask(bucket, mFilePath, "/Android/" + timestamp + nameend, "", videoAttr, to_over_write, new IUploadTaskListener() {
            @Override
            public void onUploadSucceed(final FileInfo result) {
              /*  EventBus.getDefault().post(new RefreshPostCountEvent());
                EventBus.getDefault().post(new UploadProgressEvent(title, 100, 1));*/
                Log.e("eeeeeeeeeeee", "视频上传成功");
            }

            @Override
            public void onUploadStateChange(ITask.TaskState state) {

            }

            @Override
            public void onUploadProgress(final long totalSize, final long sendSize) {
                long p = (long) ((sendSize * 100) / (totalSize * 1.0f));
             /*   EventBus.getDefault().post(new UploadProgressEvent(title, p, 1));*/
                Log.e("eeeeeeeeeeee", "视频上传进度" + title + "   " + p + "   " + 1);

            }

            @Override
            public void onUploadFailed(final int errorCode, final String errorMsg) {
                Log.e("eeeeeeeeeeee", "视频上传失败" + errorCode + "  " + errorMsg);
            }
        });

        String sign = "r9Hj2zdpy5ChptJNfKf2GdA48oVhPTEwMDEyNzkyJmI9dmlkZW8maz1BS0lEcDZsS0loRnJmNHdTTTFuYVQ0MGVjTGZBT0JTTkJNWkcmZT0xNTA3NzgyNDk3JnQ9MTUwNzc3NTI5NyZyPTU3NjAyNTM4MSZmPQ==";
        videoUploadTask.authIsEmpty();
        videoUploadTask.setAuth(sign);
        Log.e("sign :", sign);
        videoUploadTask.setBusiMsg(videoUploadInfoJson);
        mFileUploadManager.upload(videoUploadTask); //开始上传

    }
}
